package org.extjs.filetree;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileWrapper {
	private static Log log = LogFactory.getLog(FileWrapper.class);
	private static String iconPrefix = "file";
	private int chunkSize = 1024;
	public static ResourceBundle messages = ResourceBundle.getBundle("filetree",Locale.getDefault());
	public static ResourceBundle FILE_EXTENSIONS = ResourceBundle.getBundle("mimetypes",Locale.getDefault());
	private static String baseDir =messages.getString("filetree.root");
	private String extension=null;

	private String clientName;
	private File file;
	public FileWrapper(String clientName) {
		this.clientName = clientName;
		String localPath = null;
        //remove the "root" from the begining
		localPath = clientName.substring(4, clientName.length());
		localPath.replace('/',File.separatorChar);
		localPath =  baseDir + localPath;
		log.info("Server name for " + clientName +" is :" + localPath);
		file = new File(localPath);
		String fileName = file.getName();
		String[] tmp =fileName.split("\\.");
		extension = tmp[tmp.length-1];
	}
	
	public FileWrapper(File file){
		this.file = file;
		//TODO calculate the clientName here, not needed as of now.
	}
	
	public boolean isDirectory(){
		return file.isDirectory();
	}
	
	public List listFiles(){
		if(!isDirectory()) return null;
		List fileList = new LinkedList();
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			fileList.add(new FileWrapper(files[i]));
		}
		return fileList;
	}

	public boolean delete() {
		boolean result = true;
		if (isDirectory()) {
			Iterator itr = listFiles().iterator();
			while(itr.hasNext()){
				FileWrapper fileWrap = (FileWrapper)itr.next();
				if(result) result = fileWrap.delete();
			}
		} else {
			result = file.delete();
		}
		return result;
	}
	
	public Map getJSONMap() {
		Map param = new HashMap();
		param.put("text", file.getName());
		param.put("disabled", Boolean.FALSE);
		param.put("qtip", "Size: " + file.length());
		param.put("leaf", new Boolean(!file.isDirectory()));
		param.put("iconCls", getFileIcon());
		return param;
	}
	
	public boolean renameTo(String newName){
		boolean success = false;
		FileWrapper to = new FileWrapper(newName);
		success = file.renameTo(to.getFile());
		return success;
	}
	
	public String getFileIcon() {
		String icnCls = null;
		if (isDirectory()) {
			icnCls = "folder";
		} else {
			String fileName = file.getName();
			
			if(extension==null || extension.length()<1){
				icnCls =  iconPrefix;
			}else{
				icnCls = iconPrefix + "-" + extension.toLowerCase();
			}
		}
		return icnCls;
	}
	
	public String getMimeType(){
		String mimeType = null;
		try {
			mimeType = FILE_EXTENSIONS.getString(extension.toLowerCase());
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		if(mimeType==null || mimeType.length()<1){
			mimeType = "text/plain";
		}
		return mimeType;
	}
	
	public String getFileName(){
		return file.getName();
	}
	public void upload(InputStream is) throws Exception{
		OutputStream os = new FileOutputStream(file);
		byte[] chunk = new byte[chunkSize];
		while (is.read(chunk) != -1) {
			os.write(chunk);
		}
		is.close();
		os.close();
	}
	
	public void download(OutputStream os) throws Exception {
		InputStream is = new FileInputStream(file);
		byte[] chunk = new byte[chunkSize]; 
		while (is.read(chunk) != -1) {
			os.write(chunk);
		}
		is.close();
		os.close();
	}

	public String getClientName() {
		return clientName;
	}

	public File getFile() {
		return file;
	}	
}
